<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:15
 */
class Prodoctor_auth extends MY_Admin_Controller
{
    public function index()
    {
        $limit = 20;
        $page  = intval($this->input->get_post('page'));
        !$page && $page = 1;
        $offset = ($page - 1) * $limit;


        $uid        = (int)$this->input->get_post('uid');
        $realname   = trim($this->input->get_post('realname'));
        $status     = $this->input->get_post('status');
        $start_time = trim($this->input->get_post('start_time'));
        $end_time   = trim($this->input->get_post('end_time'));


        $where = array();
        $uid && $where['uid'] = $uid;
        !empty($realname) && $where['realname like \'%'.$realname.'%\''] = null;
        ($status || $status == '0') && $where['status'] = $status;
        (!empty($start_time)) && $where['create_time >'] = date('Y-m-d H:i:s', strtotime($start_time));
        (!empty($start_time)) && $where['create_time <'] = date('Y-m-d H:i:s', strtotime($end_time));


        $this->load->model('user/Model_user_prodoctor_auth','urp');
        $total = $this->urp->count($where);
        $data  = $this->urp->lists($offset, $limit, $where, true, 'status ASC, aid DESC');


        $config['total_rows'] = $total;
        $config['per_page'] = 20;
        $config['base_url'] = '/user/Prodoctor_auth/index';


        //分页
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $page_html = $this->pagination->create_links();


        $uids = $sids = array();
        foreach ($data as $v)
        {
            $uids[] = $v['uid'];
            $v['audit_uid'] && $sids[] = $v['audit_uid'];
        }



        $this->load->model('user/Model_user','u');
        $user_data = $this->u->getByIn('uid', $uids);


        $this->load->model('staff/Model_staff','s');
        $staff_data = $this->s->getByIn('sid', $sids);


        $data = array
        (
            'data'       => $data,
            'total_rows' => $total,
            'page_html'  => $page_html,
            'user_data'  => $user_data,
            'staff_data' => $staff_data,

            'uid'        => $uid,
            'realname'   => $realname,
            'start_time' => $start_time,
            'end_time'   => $end_time,
            'status'     => $status,
        );


        $this->load->view('admin/user/prodoctor_auth/index',$data);
    }

    /*
     *@fun 审核，保存审核状态
     * @return Aarray();
    */
    function save_status(){

        $response = array('code' => '1', 'msg' => '审核成功');


        do
        {
            $aid    = (int)$this->input->get_post('aid');
            $status = (int)$this->input->get_post('status');


            if (!$aid)
            {
                $response = error(100003);
                break;
            }


            $this->load->model('user/Model_user_prodoctor_auth', 'prodoctor');
            $real_data = $this->prodoctor->getById($aid);
            if (empty($real_data))
            {
                $response = error(10075);
                break;
            }


            $this->load->model('user/Model_user', 'u');
            $user_item = $this->u->getById($real_data['uid']);
            if (empty($user_item))
            {
                $response = error(10002);
                break;
            }


            if ($real_data['status'] == $status)
            {
                $response = error(10076);
                break;
            }


            $item = array
            (
                'status'     => $status,
                'audit_uid'  => $this->uInfo['sid'],
                'audit_time' => now_time(),
            );


            $save_rs = $this->prodoctor->save($item, $aid);
            if (!$save_rs)
            {
                $response = error(10077);
                break;
            }


            $sms_template = config_item('sms_template');
            $this->load->library('sms/sms');
            switch ($status)
            {
                case USER_PRODOCTOR_AUTH_STATUS_NORMAL:      #认证通过发送消息
                    $this->load->service('message/service_message_push');
                    $this->service_message_push->prodoctor_auth_success($real_data['uid']);
                    break;

                case USER_PRODOCTOR_AUTH_STATUS_FAIL:         #审核失败发送消息
                    $this->load->service('message/service_message_push');
                    $this->service_message_push->prodoctor_auth_fail($real_data['uid']);
                    break;
            }

        } while (false);


        $this->json_output($response);

    }

    /*
    *@fun 职医认证编辑
    *@return Json
    */
    public function create()
    {
        $aid = (int)$this->uri->segment(4);


        //实名认证信息
        $this->load->model('user/Model_user_prodoctor_auth', 'aup');
        $userInfo   = $this->aup->getById($aid);


        //获取该用户个人的信息;
        $this->load->model('user/Model_user','user');
        $user_list = $this->user->getOne(array('uid'=>$userInfo['uid']));
        $userInfo['uname'] = $user_list['uname'];


        //获取员工信息取得审核人名称
        $this->load->model('staff/Model_staff','staff');
        $staff = $this->staff->getById($userInfo['audit_uid']);
        $userInfo['audit_uname'] = $staff['login_name'];


        $data = array
        (
            'userInfo' => $userInfo,
        );


        $this->load->view('admin/user/prodoctor_auth/create',$data);
    }

    /*
     * 职医认证编辑后保存
     */
    public function save()
    {


        $aid = (int)$this->input->get_post('aid');

        $data = array(
            'realname'      =>      $this->input->get_post('rname'),
            'id_card'       =>      trim($this->input->get_post('id_card')),
            'fid'      =>           (int)$this->input->get_post('fid'),
            //'status'        =>      (int)$this->input->get_post('status'),
            'audit_opinion' =>      trim($this->input->get_post('audit_opinion')),
        );

        //更新user_info表
        $this->load->model('user/Model_user_prodoctor_auth', 'aup');

        $realData = $this->aup->getById($aid);

        //判断审核状态或者审核意见是否有变更
        if($realData['status'] != $data['status'] || $realData['audit_opinion'] != $data['audit_opinion'])
        {
            $data['audit_uid'] = $this->uInfo['sid'];
            $data['audit_time'] = now_time();
        }


        $this->aup->save($data, $aid);


        $this->load->helper('url');
        redirect(url('admin').'user/prodoctor_auth/index');
    }

    /*
    * @fun 删除该条审核信息
    */

    function delete(){

        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $aid = (int)$this->input->get_post('aid');

            if (!$aid)
            {
                $response = error(10031);
                break;
            }

            $this->load->model('user/model_user_prodoctor_auth', 'aup');

            $res = $this->aup->delete(array('aid' => $aid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }

}